﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;

namespace LitTools
{
    public class ExcelConverterWindow : EditorWindow
    {
        private Rect leftRect;
        private Rect rightRect;
        private Vector2 scrollPos;
        private ExcelConverter selectedConverter;

        private GUIStyle convertBtnNormalStyle;
        private GUIStyle convertBtnSelectedStyle;
        private GUIStyle rightAreaStyle;
        [MenuItem("Tools/LitExcel/转换器总览")]
        public static void OpenWindow()
        {
            var window = EditorWindow.GetWindow<ExcelConverterWindow>();
            var guiContent = new GUIContent("Excel转换器总览");
            window.titleContent = guiContent;
            window.Show();
        }
        private void OnEnable()
        {
            convertBtnNormalStyle = new GUIStyle("Button");
            convertBtnNormalStyle.normal.background = Texture2D.grayTexture;
            convertBtnNormalStyle.normal.textColor = Color.white;
            convertBtnNormalStyle.fontSize = 15;
            convertBtnNormalStyle.fixedHeight = 30;

            convertBtnSelectedStyle = new GUIStyle("Button");
            convertBtnSelectedStyle.normal.background = Texture2D.blackTexture;
            convertBtnSelectedStyle.normal.textColor = Color.white;
            convertBtnSelectedStyle.fontSize = 15;
            convertBtnSelectedStyle.fixedHeight = 30;

            rightAreaStyle = new GUIStyle("window");
        }
        private void OnGUI()
        {
            leftRect = new Rect(0, 25, position.width * 0.4f, position.height - 25);
            rightRect = new Rect(position.width * 0.4f, 25, position.width * 0.6f, position.height - 25);
            convertBtnNormalStyle.fixedWidth = position.width * 0.4f;
            convertBtnSelectedStyle.fixedWidth = position.width * 0.4f;

            GUILayout.BeginVertical();
            DrawToolBar();//绘制toolBar
            GUILayout.BeginHorizontal();
            GUILayout.BeginArea(leftRect);
            DrawLeft();//绘制左侧区域
            GUILayout.EndArea();
            GUILayout.BeginArea(rightRect, rightAreaStyle);
            DrawRight();//绘制右侧区域
            GUILayout.EndArea();
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
        }

        private void DrawToolBar()
        {
            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            GUIStyle btnStyle = new GUIStyle("Button")
            {
                stretchHeight = false,
                stretchWidth = false,
                alignment = TextAnchor.MiddleLeft,

            };
            if (GUILayout.Button("刷新", btnStyle))
            {

            }
            GUILayout.EndHorizontal();
        }

        private void DrawLeft()
        {
            scrollPos = GUILayout.BeginScrollView(scrollPos);
            foreach (var item in GetAllExcelConverters())
            {
                if (selectedConverter == item)
                {
                    if (GUILayout.Button(item.name, convertBtnSelectedStyle))
                    {
                        GUIUtility.keyboardControl = 0;
                        selectedConverter = item;
                    }
                }
                else
                {
                    if (GUILayout.Button(item.name, convertBtnNormalStyle))
                    {
                        GUIUtility.keyboardControl = 0;
                        selectedConverter = item;
                    }
                }
                GUILayout.Space(2);
            }
            GUILayout.EndScrollView();
        }
        private List<ExcelConverter> GetAllExcelConverters()
        {
            var result = new List<ExcelConverter>();
            var guids = AssetDatabase.FindAssets($"t:{typeof(ExcelConverter)}");
            var pathes = guids.ToList().Select(g => AssetDatabase.GUIDToAssetPath(g));
            foreach (var path in pathes)
            {
                result.Add(AssetDatabase.LoadAssetAtPath<ExcelConverter>(path));
            }
            return result;
        }

        private void DrawRight()
        {
            if (selectedConverter != null)
            {
                var editor = Editor.CreateEditor(selectedConverter);
                editor.OnInspectorGUI();
                if (GUILayout.Button("定位资源"))
                {
                    Selection.activeObject = selectedConverter;
                }
            }
        }
    }
}
